package com.we823.utils.web;

import java.text.ParseException;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;

public class RequestUtil {
	
	/**
	 * 在request中根据name查找值
	 * @param request
	 * @param columnName 参数名
	 * @return 
	 */
	public static String getValueByParam(HttpServletRequest request,String columnName) {
		return request.getParameter(columnName);
	}
	/**
	 * 在request中根据name查找值,如找不到就返回默认值
	 * @param request
	 * @param columnName
	 * @param defaultValue
	 * @return
	 */
	public static Integer getValueByParam(HttpServletRequest request,String columnName,Integer defaultValue) {
		Integer iValue = defaultValue;
		String sValue = request.getParameter(columnName);
		if(sValue!=null){
			if(StringUtils.isNumeric(sValue)){
				iValue=Integer.parseInt(sValue);
			}
		}
		return iValue;
	}

	public static boolean getBooleanValue(HttpServletRequest request,String column) {
		if(request.getParameter(column)==null) {
			return false;
		}
		if("true".equals(request.getParameter(column).trim().toLowerCase())) {
			return true;
		}
		return false;
	}
	@SuppressWarnings("unchecked")
	public static <T> T getValueByParam(HttpServletRequest request,String columnName, T defaultValue){
		T dValue = defaultValue;
		String sValue = request.getParameter(columnName);
		if(defaultValue instanceof String){
			if(sValue!=null){
				return (T) sValue;
			}else{
				return defaultValue;
			}
		}else if(defaultValue instanceof Integer){
			Integer iValue = -1;
			if(sValue!=null){
				if(StringUtils.isNumeric(sValue)){
					iValue=Integer.parseInt(sValue);
				}
			}
			return (T) iValue;
		}else if(defaultValue instanceof Boolean){
			if(sValue!=null){
				if("true".equals(sValue.trim().toLowerCase())){
					return (T) Boolean.TRUE;
				}
			}
		}else if(defaultValue instanceof Date){
			if(sValue!=null){
				String Date_Format="yyyy-MM-dd hh:mm:ss";
				try {
					return (T) DateUtils.parseDate(sValue,Date_Format);
				} catch (ParseException e) {
					return defaultValue;
				}
			}
		}else if(defaultValue instanceof Float){
			if(sValue!=null){
				float f = 0f;
				try{
					f = Float.parseFloat(sValue);
				}catch(Exception ex){
					f=0f;
				}
				return (T) new Float(f);
			}
			return defaultValue;
		}
		return dValue;
	}
	
	@SuppressWarnings("unchecked")
	public static <T> T getValueByParam(HttpServletRequest request,String columnName, Class<T> classname){
		String sValue = request.getParameter(columnName);
		if(classname.equals(String.class)){
			if(sValue!=null){
				return (T) sValue;
			}else{
				return null;
			}
		}else if(classname.equals(Integer.class)){
			Integer iValue = -1;
			if(sValue!=null){
				if(StringUtils.isNumeric(sValue)){
					iValue=Integer.parseInt(sValue);
				}
			}
			return (T) iValue;
		}
		return null;
	}
	
	public static boolean getPageable(HttpServletRequest request) {
		String pageableString = RequestUtil.getValueByParam(request, "pageable", "true");
		if("false".equals(pageableString)){
			return false;
		}
		return true;
	}
	
	public static String[] getValuesByParam(HttpServletRequest request,String name){
		String[] values = request.getParameterValues(name);
		if(values==null){
			values = new String[]{};
		}
		return values;
	}

	public static Date getDateValueByParam(HttpServletRequest request,String name,String format){
		Date date = new Date();
		String sValue = request.getParameter(name);
		if(sValue!=null){
			if(format==null){
				format = "yyyy-MM-dd HH:mm:ss";
			}
			try {
				date = DateUtils.parseDate(sValue, format);
			} catch (ParseException e) {
				date = new Date();
			}
		}
		return date;
	}
    public static String getIp4(HttpServletRequest request) {
        String ip = request.getHeader("x-forwarded-for");
        if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        return ip;
    }
	public static Float getFloatValueByParam(HttpServletRequest request,String param) {
		return getFloatValueByParam(request, param,0);
	}
	public static float getFloatValueByParam(HttpServletRequest request,String param,float defautlValue) {
		String valueString = getValueByParam(request, param);
		float result = 0;
		try {
			result = Float.parseFloat(valueString);
		}catch(Exception ex) {
			result = defautlValue;
		}
		return result;
	}
}
